package ui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

import ui.RRulePanel.RRulePanelListener;
import entity.EntityConstants;
import entity.ExDate;
import entity.RecurrenceRule;

/**
 * A Panel that contains options to generate and modify 
 * recurrence rules with the "monthly" frequency.
 * 
 * @author Sean Young
 * @version 1.0
 *
 */
public class MonthlyExPanel extends JPanel implements EntityConstants, ActionListener{
	private JPanel buttonPanel, monthPanel, cardPanel;
	ArrayList<JCheckBox> monthBoxes;
	private CreateEventPanel parent;
	private ExDate monthException = new ExDate();
	private JButton done, cancel;
	private JFrame container;
	private MonthlyExListener listener;
	
	/**
	 * Constructor for the MonthlyExPanel. Requires a reference
	 * to the main panel of the CalendarUI.
	 * @param parent The main panel
	 */
	public MonthlyExPanel(CreateEventPanel parent, JFrame container) {
		super();
		setLayout(new BorderLayout());
		this.parent = parent;
		this.container = container;
		listener = new MonthlyExListener();
		
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		setPreferredSize(new Dimension(270, 100));

		
		monthBoxes = new ArrayList<JCheckBox>();
		for(int i = 0; i < 12; i++) {
			monthBoxes.add(new JCheckBox());
		}
		
		
		monthPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		monthPanel.add(new JLabel("Months to Exclude"));
		monthPanel.add(monthBoxes.get(INDEX_JANUARY));
		monthPanel.add(new JLabel("January "));
		monthPanel.add(monthBoxes.get(INDEX_FEBRUARY));
		monthPanel.add(new JLabel("February "));
		monthPanel.add(monthBoxes.get(INDEX_MARCH));
		monthPanel.add(new JLabel("March        "));
		monthPanel.add(monthBoxes.get(INDEX_APRIL));
		monthPanel.add(new JLabel("April "));
		monthPanel.add(monthBoxes.get(INDEX_MAY));
		monthPanel.add(new JLabel("May "));
		monthPanel.add(monthBoxes.get(INDEX_JUNE));
		monthPanel.add(new JLabel("June "));
		monthPanel.add(monthBoxes.get(INDEX_JULY));
		monthPanel.add(new JLabel("July "));
		monthPanel.add(monthBoxes.get(INDEX_AUGUST));
		monthPanel.add(new JLabel("August                    "));
		monthPanel.add(monthBoxes.get(INDEX_SEPTEMBER));
		monthPanel.add(new JLabel("September  "));
		monthPanel.add(monthBoxes.get(INDEX_OCTOBER));
		monthPanel.add(new JLabel("October "));
		monthPanel.add(monthBoxes.get(INDEX_NOVEMBER));
		monthPanel.add(new JLabel("November "));
		monthPanel.add(monthBoxes.get(INDEX_DECEMBER));
		monthPanel.add(new JLabel("December "));
		
		add(monthPanel);
		
		Integer[] intervalOptions = new Integer[30];
		for(int i = 0; i < 30; i++) {
			intervalOptions[i] =  i + 1;
		}

		buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		done = new JButton("Done");
		done.setActionCommand("done");
		done.addActionListener(listener);
		buttonPanel.add(done);
		
		cancel = new JButton("Cancel");
		cancel.setActionCommand("cancel");
		cancel.addActionListener(listener);
		buttonPanel.add(cancel);
		
		add(buttonPanel, BorderLayout.SOUTH);
	}
	
	/**
	 * Generates a EXDATE with the specified properties.
	 * @return A recurrence rule with a weekly frequency
	 */
	public ExDate buildExDate() {
		ExDate rrule = new ExDate();
		ArrayList<String> months = new ArrayList<String>();
		
		
		for(int i = 0; i < 11; i++) {
			if(monthBoxes.get(i).isSelected()) {
				switch(i) {
					case INDEX_JANUARY:    months.add("01");
									      break;
					case INDEX_FEBRUARY:   months.add("02");
									      break;
					case INDEX_MARCH:   months.add("03");
									      break;
					case INDEX_APRIL: 	months.add("04");
										  break;
					case INDEX_MAY:  	months.add("05");
										  break;
					case INDEX_JUNE:    months.add("06");
										  break;
					case INDEX_JULY:  	months.add("07");
										  break;
					case INDEX_AUGUST:  months.add("08");
										  break;
					case INDEX_SEPTEMBER:  months.add("09");
										  break;
					case INDEX_OCTOBER:  months.add("10");
										  break;
					case INDEX_NOVEMBER:  months.add("11");
										  break;
					case INDEX_DECEMBER:  months.add("12");
										  break;
				}
			}
		}
		
		if(months.size() > 0) {
			this.monthException.addMonths(months);
		}
		
		return rrule;
	}
	
	
	/**
	 * Creates and displays the EXDATE panel as a pop-up 
	 * window.
	 * @param parent The CreateEventPanel
	 * @return The MonthlyExPanel
	 */
	public static MonthlyExPanel showExPanelWindow(CreateEventPanel parent){
		JFrame frame = new JFrame("Repeat Exceptions");
		frame.setSize(new Dimension(355, 255));
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		MonthlyExPanel window = new MonthlyExPanel(parent, frame);
		ImageIcon icon = new ImageIcon("images/Calendar-3-256x256.png");
		frame.setIconImage(icon.getImage());
		frame.add(window);
		frame.setLocationRelativeTo(parent);
		frame.setResizable(false);
		frame.setVisible(true);
		return window;
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		switch(e.getActionCommand()){
		case "done" :  
					   break;
		case "cancel": super.setVisible(false);
					   break;
								  
		}}
		

	/**
	 * Action listener that performs actions based upon the 
	 * button pressed in the RRulePanel.
	 *
	 */
	public class MonthlyExListener implements ActionListener{
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				switch(e.getActionCommand()){
					case "done" :  buildExDate();
								   container.dispose();
								   break;
					case "cancel": container.dispose();
								   break;
											  
				}
			}	
		}
		
		
}
